package com.yssoft.service.inventory;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.inventory.TWarehouse;
import com.yssoft.mapper.core.TCodeMapper;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.inventory.TInstockApplyMapper;
import com.yssoft.mapper.inventory.TWarehouseMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: WarehouseService
 * @类描述: 仓库service
 * @作者: 王永宁
 * @创建时间:2018年5月12日 下午1:31:58
 */
@Service
public class WarehouseService {

    @Resource
    private TWarehouseMapper warehouseMapper;
    @Resource
    private TInstockApplyMapper applyMapper;

    @Resource
    private TPersonnelMapper personMapper;
    @Resource
    private TCodeMapper codeMapper;

    /**
     * 获取仓库列表
     *
     * @param companyId
     * @param name
     * @param tcId
     * @return
     * @author 王永宁
     */
    public List<Map<String, Object>> findAll(int page, int limit, Integer companyId, String name, String tcId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("tcId", tcId);
        param.put("companyId", companyId);
        param.put("name", name);
        if (limit > 0) {
            int start = ((page - 1) * limit);
            int end = limit;
            param.put("start", start);
            param.put("end", end);
        } else {
            param.put("start", null);
            param.put("end", null);
        }
        List<Map<String, Object>> list = warehouseMapper.selectWarehouseList1(param);
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> personList = warehouseMapper.queryPersonByWarehouseId(Integer.valueOf(map.get("id").toString()));
            map.put("personList", personList);
        }
        return list;
    }

    /**
     * 获取仓库列表ck
     *
     * @param companyId
     * @param name
     * @param tcId
     * @return
     * @author 王永宁
     */
    public List<Map<String, Object>> findAllck(int page, int limit, Integer companyId, String name, String tcId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("tcId", tcId);
        param.put("companyId", companyId);
        param.put("name", name);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<Map<String, Object>> list = warehouseMapper.selectWarehouseListck(param);
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> personList = warehouseMapper.queryPersonByWarehouseId(Integer.valueOf(map.get("id").toString()));
            map.put("personList", personList);
        }
        return list;
    }

    /**
     * 查询仓库明细
     *
     * @param warehouseId
     * @return
     */
    public Map<String, Object> queryWarehouseDetail(Integer warehouseId) {
        Map<String, Object> map = warehouseMapper.queryWarehouseInfo(warehouseId);
        List<Map<String, Object>> personList = warehouseMapper.queryPersonByWarehouseId(Integer.valueOf(map.get("id").toString()));
        map.put("personList", personList);
        //根据库存类型，查询库存明细
        //物品类型：1-成品 2-半成品 3-物料
//        if (type.equals("1")) {
            //查询成品明细
            List<Map<String, Object>> mapList = applyMapper.queryWarehouseProductOrMaterialList(Integer.valueOf(map.get("id").toString()));
            map.put("detailedList", mapList);
//        } else if (type.equals("2")) {
//            //查询半成品明细
//            List<Map<String, Object>> mapList = applyMapper.queryWarehouseSemiFinishedGoodsList(Integer.valueOf(map.get("id").toString()));
//            map.put("detailedList", mapList);
//        } else {//查询物料明细
//            List<Map<String, Object>> mapList = applyMapper.queryWarehouseMaterialList(Integer.valueOf(map.get("id").toString()));
//            map.put("detailedList", mapList);
//        }
        return map;
    }


    /**
     * @param companyId 企业id
     * @param ids       人员id
     * @return Object
     * @方法名称: getPersonListByEId
     * @功能描述: 根据企业id查询人员列表
     * @作者: 王永宁
     * @创建时间:2018年5月17日 上午9:33:38
     */
    public List<Map<String, Object>> getPersonListByEId(Integer companyId,
                                                        String id) {
        HashMap<String, Object> param = new HashMap<>();
        if (id != null && id != "") {
            String[] ids = id.split(",");
            param.put("ids", ids);
        }
        param.put("companyId", companyId);
        return personMapper.selectPersonForWarehouse(param);
    }

    /**
     * @param id 仓库id
     * @return Object
     * @方法名称: getOne
     * @功能描述: 根据仓库id查询仓库
     * @作者: 王永宁
     * @创建时间:2018年5月17日 上午10:33:38
     */
    public Map<String, Object> getOne(String id) {
        HashMap<String, Object> warehouse = new HashMap<String, Object>();
        warehouse.put("warehouse", warehouseMapper.selectByPrimaryKey(Integer.valueOf(id)));
        warehouse.put("personList", warehouseMapper.queryPersonByWarehouseId(Integer.valueOf(id)));
        return warehouse;
    }

    /**
     * @param id 仓库id
     * @param id startupStat
     * @return Object
     * @方法名称: startup
     * @功能描述: 更改启用停用状态
     * @作者: 王永宁
     * @创建时间:2018年5月17日 上午10:33:38
     */
    public void startup(String id, Integer startupStat) {
        TWarehouse warehouse = warehouseMapper.selectByPrimaryKey(Integer.valueOf(id));
        if (startupStat == 0) {
            warehouse.setStartupStat("1");
        } else {
            warehouse.setStartupStat("0");
        }
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    /**
     * @param warehouse 仓库实体
     * @param id        人员id
     * @return Object
     * @方法名称: saveWarehouse
     * @功能描述: 保存仓库
     * @作者: 王永宁
     * @创建时间:2018年5月17日 上午11:04:38
     */
    @Transactional
    public void saveWarehouse(TWarehouse warehouse, String ids) {
        Integer wId;
        if (warehouse.getId() != null) {
            wId = warehouse.getId();
            warehouseMapper.updateByPrimaryKeySelective(warehouse);
            warehouseMapper.deletePersonForWarehouse(warehouse.getId());
        } else {
            warehouse.setCreateTime(new Date());
            warehouseMapper.insertSelective(warehouse);
            wId = warehouse.getId();
        }
        HashMap<String, Object> param = new HashMap<>();
        if (ids != null && ids != "") {
            String[] idss = ids.split(",");
            param.put("ids", idss);
            param.put("wId", wId);
            warehouseMapper.saveWarehouseAndPerson(param);
        }

    }

    /**
     * @return Object
     * @方法名称: findWarehouseType
     * @功能描述: 获取仓库类型
     * @作者: 于兴达
     * @创建时间:2018年9月06日 下午2:35:38
     */
    public List<TCode> findWarehouseType() {
        HashMap<String, Object> param = new HashMap<>();
//		param.put("classify", "stockType");
        param.put("classify", "material");
        return codeMapper.findCodeByParam(param);
    }

    /**
     * @return List<Map < String, Object>>
     * @方法名称: findNoAll
     * @功能描述: 查询不合格品仓库
     * @作者: 王永宁
     * @创建时间:2018年9月8日09:00:47
     */
    public List<Map<String, Object>> findNoAll(Integer companyId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("described", "不合格品");
        param.put("companyId", companyId);
        List<Map<String, Object>> list = warehouseMapper.selectNoWarehouseList(param);
        return list;
    }

}


